class Solution:
    def isValidBST(self, root: Optional[TreeNode]) -> bool:
        max_val = inf
        min_val = -inf
        return self.isVal(root, min_val, max_val)
    def isVal(self, root, min_val, max_val):
        if root is None:
            return True
        if root.val < max_val and root.val > min_val:
            pass
        else:
            return False

        if not self.isVal(root.left, min_val, root.val) :
            return False
        if not self.isVal(root.right, root.val, max_val) :
            return False
        return True
        #上下界
'''
class Solution:
    pre = -inf
    def isValidBST(self, root: Optional[TreeNode]) -> bool:
        if root is None:
            return True
        if not self.isValidBST(root.left):  # 左
            return False
        if root.val <= self.pre:  # 中
            return False
        self.pre = root.val
        return self.isValidBST(root.right)  # 右
'''